﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using MixScriptWeb.control;
using MixScriptWeb.Share;
using MixScript.BLL;
namespace MixScriptWeb.Referensi
{
    public partial class fMstAgama : cCommonWeb, IMIxCommon //System.Web.UI.Page
    {

        private static DataTable dt;
        private static SortDirection sd;
        private static cReferences oReff;
        // private static Control[] ctl;

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (!Page.IsPostBack)
                {
                    addFindClickAttribute(this, new MixScriptTB[] { txta_code }, new string[] { "SRCAGAMAID" });
                    //  setSortExpression(gvdetail, new string[] { "banknum", "bankname", "address", "city", "country", "ac" });

                }
                mn = (MainMenu)this.Master;
                hideStatusBar();
                oReff = new cReferences("MST_AGAMA", "AGAMAID");
                // if(gvdetail.Rows.Count>0)


            }
            catch (Exception ex)
            {

                ShowStatusBar(ex.Message, msgType.msgError);
            }


        }

        protected void gvdetail_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {

                //oUM = new cUserManagement();

                if (gvdetail.SelectedIndex > -1)
                {
                    txta_code.Text = gvdetail.SelectedRow.Cells[1].Text;
                    txtjenis_agama.Text = gvdetail.SelectedRow.Cells[2].Text;
                    txtketerangan.Text = gvdetail.SelectedRow.Cells[3].Text;
                   
                    //ini ngehapus &nbsp;
                    removeNbsp(new MixScriptTB[] { txta_code, txtjenis_agama, txtketerangan });



                }
            }
            catch (Exception ex)
            {

                ShowStatusBar(ex.Message);
            }
        }

        protected void gvdetail_Sorting(object sender, GridViewSortEventArgs e)
        {
            if (sd == SortDirection.Ascending)
            {
                sd = SortDirection.Descending;
            }
            else
            {
                sd = SortDirection.Ascending;
            }

            DataView dataView = new DataView(dt);
            dataView.Sort = e.SortExpression + " " + ShortDirection(sd);

            bindGridview(gvdetail, dataView);
        }

        protected void gvdetail_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {

            gvdetail.PageIndex = e.NewPageIndex;
            //gvdetail.DataBind();
            bindGridview(gvdetail, dt);

        }

        protected void btnFind_Click(object sender, EventArgs e)
        {
            try
            {
                oReff = new cReferences(); //new cUserManagement();
                dt = new DataTable();
                Control[] ctl = new Control[] { txta_code, txtjenis_agama, txtketerangan };
                if (oReff.getInquiry(ref dt, genInqParam(ctl), "mst_agama", out errMsg))
                {

                }
                else
                {
                    ShowStatusBar("Record Not Found", msgType.msgError);
                }
                bindGridview(gvdetail, dt);



            }
            catch (Exception ex)
            {

                ShowStatusBar(ex.Message, msgType.msgError);
            }

        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                oReff = new cReferences();
                string ss = "";
                if (isvalid())
                {
                    if (txta_code.Text.Trim() == string.Empty)
                    {
                        if (oReff.AddNewRowDB(out errMsg, "MST_AGAMA", new int[] { 0 }, new string[] { "AGAMAID" }, false, txta_code, txtjenis_agama, txtketerangan ))
                        {
                            ShowStatusBar(errMsg, msgType.msgError);
                            txta_code.Text = ss;
                            btnFind_Click(btnFind, new EventArgs());
                            btnClear_Click(btnClear, new EventArgs());
                        }
                        else
                        {
                            ShowStatusBar(errMsg, msgType.msgError);
                        }

                    }
                    else
                    {
                        if (oReff.isRecordExist("MST_AGAMA", new string[] { "agamanum" }, new object[] { txta_code.Text }))
                        {
                            if (oReff.EditRowDtDB(out errMsg, "MST_AGAMA", new string[] { "AGAMANUM" }, new object[] { txta_code.Text }, false, txta_code, txtjenis_agama, txtketerangan ))
                            {
                                ShowStatusBar(errMsg, msgType.msgError);
                                btnFind_Click(btnFind, new EventArgs());
                                btnClear_Click(btnClear, new EventArgs());
                            }
                            else
                            {
                                ShowStatusBar(errMsg, msgType.msgError);
                            }


                        }
                        else
                        {
                            ShowStatusBar("Record Not Found");
                        }
                    }
                }
                else
                {
                    ShowStatusBar(errMsg);
                }
            }
            catch (Exception ex)
            {

                ShowStatusBar(ex.Message);
            }
        }



        protected void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                //cek apakah user sudah pilih data yg akan dihapus
                if (txta_code.Text != string.Empty)
                {
                    //cek apakah data itu ada
                    if (oReff.isRecordExist("MST_AGAMA", new string[] { "AGAMANUM" }, new object[] { txta_code.Text }))
                    {
                        oReff = new cReferences();

                        //coba hapus
                        //                                nama primary key                nilainya          nama tablenya   pesan error
                        if (oReff.deleteRowDB(new string[] { "AGAMANUM" }, new object[] { txta_code.Text }, "MST_AGAMA", out errMsg))
                        {
                            //jika berhasil
                            ShowStatusBar(em.getError(8));
                        }
                        else
                        {   //jika gagal
                            ShowStatusBar(errMsg);
                        }

                    }
                    else
                    {   //jika data tidak ditemukan
                        ShowStatusBar(em.getError(7));
                    }
                }
                else
                {
                    ShowStatusBar(em.getError(7));
                }
            }
            catch (Exception ex)
            {

                ShowStatusBar(ex.Message);
            }
        }

        protected void btnClear_Click(object sender, EventArgs e)
        {
            try
            {
                errMsg = "";
                setValid(new MixScriptTB[] { txta_code, txtjenis_agama, txtketerangan });
                clearMixTB(new MixScriptTB[] { txta_code, txtjenis_agama, txtketerangan });
            }
            catch (Exception ex)
            {

                ShowStatusBar(ex.Message);
            }

        }



        #region IMIxCommon Members

        //validasi inputan
        public bool isvalid()
        {
            try
            {
                errMsg = "";
                //set semua control valid
                setValid(new MixScriptTB[] { txta_code, txtjenis_agama, txtketerangan });
                //cek semua field mandatory
                if (MustFilledOk(new Control[] { txta_code, txtjenis_agama, txtketerangan }, out errMsg))
                {

                }
                else
                {
                    ShowStatusBar(errMsg + " " + em.getError(2));
                    return false;
                }
                return true;
            }
            catch (Exception ex)
            {

                throw ex;
            }

        }

        #endregion
    }
}
